<template>
  <div class="banner2">
    <div class="carousel">
      <h1
        class="animate__animated animate__fast"
        :class="{ animate__fadeInUp: inAnimation }"
        @animationend="inAnimation = false"
      >{{ banner.title }}</h1>
      <p
        class="animate__animated animate__fast"
        :class="{ animate__fadeInUp: inAnimation }"
      >{{ banner.desc }}</p>
      <div class="bottom">
        <div class="pagination">
          <i class="el-icon-back"></i>
          <div class="page">{{ currentIndex + 1 }}/{{ list.length }}</div>
          <i class="el-icon-right"></i>
        </div>
        <div class="indecation">
          <span
            v-for="i in list?.length"
            :key="i"
            class="indecation-item"
            :class="{ current: currentIndex === i - 1 }"
            @click="switchBanner(i - 1)"
          ></span>
        </div>
      </div>
    </div>
    <div class="round" :style="`border-${direction}-color: rgb(118, 104, 203);`">
      <span class="round-span round-span-tr active" :style="styleObject.top"></span>
      <span class="round-span round-span-br active" :style="styleObject.right"></span>
      <span class="round-span round-span-bl" :style="styleObject.bottom"></span>
      <span class="round-span round-span-tl" :style="styleObject.left"></span>
      <div
        @click="switchRound('top')"
        class="round-item round-item-t"
        :class="{ current: direction === 'top' }"
      >
        <img v-if="direction === 'top'" src="@/static/img/icon/round_cart_active.png" />
        <img v-else src="@/static/img/icon/round_cart.png" />
        <h1>商城</h1>
      </div>
      <div
        @click="switchRound('right')"
        class="round-item round-item-r"
        :class="{ current: direction === 'right' }"
      >
        <img v-if="direction === 'right'" src="@/static/img/icon/round_horn_active.png" />
        <img v-else src="@/static/img/icon/round_horn.png" />
        <h1>推广</h1>
      </div>
      <div
        @click="switchRound('bottom')"
        class="round-item round-item-b"
        :class="{ current: direction === 'bottom' }"
      >
        <img v-if="direction === 'bottom'" src="@/static/img/icon/round_gift_active.png" />
        <img v-else src="@/static/img/icon/round_gift.png" />
        <h1>活动福利</h1>
      </div>
      <div
        @click="switchRound('left')"
        class="round-item round-item-l"
        :class="{ current: direction === 'left' }"
      >
        <img v-if="direction === 'left'" src="@/static/img/icon/round_turntable_active.png" />
        <img v-else src="@/static/img/icon/round_turntable.png" />
        <h1>ROLL房福利</h1>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, toRefs, reactive, onUnmounted, watchEffect, onMounted, computed } from 'vue'
import animationSytle from './animation'
export default defineComponent({
  name: 'Banner2',
  props: {
    list: {
      type: Array,
      default: [],
      require: true
    }
  },
  setup(props) {

    const state = reactive({
      inAnimation: true,
      currentIndex: 0,
      banner: <any>{},

      direction: 'top',
      styleObject: animationSytle.top
    })

    state.banner = computed(() => props.list[state.currentIndex])

    let bannerTimer: number | undefined
    function startBanner() {
      clearInterval(bannerTimer)
      bannerTimer = setInterval(function() {
        if (state.currentIndex === (props.list.length - 1)) {
          state.currentIndex = 0
        } else {
          state.currentIndex++
        }
        state.inAnimation = true
      }, 5000)
    }


    function switchBanner(index: number) {
      state.inAnimation = true
      state.currentIndex = index
      startBanner()
    }

    const directions = ['top', 'right', 'bottom', 'left']

    let roundTimer: number | undefined
    function startRound() {
      clearInterval(roundTimer)
      roundTimer = setInterval(function() {
        let index = directions.findIndex(v => v === state.direction)
        if (index === directions.length - 1) {
          state.direction = directions[0]
        } else {
          state.direction = directions[index + 1]
        }
      }, 3000)
    }

    function switchRound(dir: string) {
      state.direction = dir
      startRound()
    }

    watchEffect(() => {
      state.styleObject = animationSytle[state.direction]
    })

    onMounted(() => {
      startBanner()
      startRound()
    })

    onUnmounted(() => {
      clearInterval(roundTimer)
    })

    return {
      ...toRefs(state),
      switchRound,
      switchBanner,
    }
  }
})
</script>

<style>
@import url("./animation.css");
</style>

<style lang="scss" scoped>
.banner2 {
  position: relative;
  z-index: 10;
  width: 1400px;
  margin: 16rem auto 0 auto;
  display: flex;
  justify-content: center;
  padding-left: 4rem;
  padding-bottom: 4rem;

  .carousel {
    position: relative;
    width: 36rem;
    height: 27rem;
    background: #36363c;
    box-shadow: 0 3px 27px 0 #1e1e24;
    padding: 3.5rem 5rem;

    h1 {
      margin-bottom: 1rem;
      font-size: 2.2rem;
    }

    p {
      font-size: 1.2rem;
    }

    .bottom {
      width: 75%;
      display: flex;
      justify-content: space-between;
      position: absolute;
      bottom: 3.8rem;

      .pagination {
        display: flex;
        justify-content: center;
        align-items: center;

        i {
          cursor: pointer;
          font-weight: 900;
          color: #939393;
          font-size: 1.125rem;
        }

        .page {
          font-size: 1.2rem;
          margin: 0 0.6rem;
          color: #979797;
        }
      }

      .indecation {
        display: flex;
        position: relative;
        top: 0.5rem;
        &-item {
          cursor: pointer;
          display: block;
          width: 10px;
          height: 10px;
          background: #e9eaeb;
          border-radius: 50%;
          margin-right: 8px;
        }
        .current {
          background-color: #7668cb;
        }
      }
    }
  }

  .round {
    position: relative;
    width: 560px;
    height: 340px;
    margin: auto;
    border: 1px solid #000;
    border-color: #444451;
    border-radius: 50%;
    transform: rotate(10deg) scale(0.9);
    transform-origin: right;

    .round-span {
      display: block;
      position: absolute;
      width: 20px;
      height: 20px;
      border-radius: 50%;
      background-color: #2b2b35;
      transform: scale(1.3);
      &.active {
        background-color: #7668cb;
      }

      &-tr {
        top: 55px;
        left: 50px;
      }

      &-br {
        top: 55px;
        left: 490px;
      }

      &-bl {
        top: 265px;
        left: 490px;
      }

      &-tl {
        top: 265px;
        left: 50px;
      }
    }

    .round-item {
      width: 141px;
      cursor: pointer;
      position: absolute;
      display: grid;
      justify-items: center;
      transform: rotate(-10deg);
      img {
        width: 100%;
        height: 141px;
      }
      h1 {
        font-size: 1.6rem;
        color: #fff;
        margin-top: 1rem;
      }

      &-t {
        top: -70.5px;
        left: 209.5px;
      }

      &-r {
        top: 99.5px;
        left: 470.5px;
      }

      &-b {
        top: 260.5px;
        left: 209.5px;
      }

      &-l {
        top: 99.5px;
        left: -65.5px;
      }
    }
  }
}
</style>